import {observable, action, /*toJS*/} from 'mobx';
import {Tools,Bubble} from 'weaver-mobile';
import { RouteLayout,SearchAdvanced,ListPage } from 'weaver-mobile-page';
import * as API from '../../api/home/accountBook';
import {toJS} from 'mobx'

const getLabel = Tools.getLabel;


export default class AccountBookStore {
    @observable loading = false;
    @observable hasRight = '';
    @observable baseRoute = '';
    @observable searchHotTerm = {};
    @observable nameQuery = '';
    @observable options = [];

    @observable dateParams = {
        year:'',
        month:''
    };

    @observable rememberGroup = [];
    
    @observable routeData = new RouteLayout.Store({route:this.baseRoute});
    @observable remindList = new ListPage.Store();

    @observable mark = {};

    //预留一份类型初始值
    initType = {};

    @observable rememberChecked = [];

    @observable reimbursable = ''; //未报销
    @observable reimbursed = ''; //已报销

    //发起报销流程 按钮
    @observable remindBtns = [];
    // @observable allAccountData = [];

    @observable workflowSelect = [];
    @observable wfInfo = {};

    //跳转到 记一笔详情页面
    @observable toRememberDetailPageRoute = new RouteLayout.Store({id:'toRememberDetailPageRoute',route:'/accountBook'});
    @observable toRememberDetailPageData = {};
    
  

    initData = (value={}) =>{
        this.getReimbursementReminderAll(value);
        //获取高级搜索表单
        this.getReimbursementReminderAS();

        this.rememberGroup = [
            { title: '日期' ,workflowid:'',key: "time",mark:0 },
            { title: '金额' ,workflowid:'',key: "amount",mark:'' },
            { title: '类型' ,workflowid:'',key: "costType",mark:'' },
        ];

        this.mark = this.joinObj(this.rememberGroup,'mark');
    }

    //高级搜索
    getReimbursementReminderAS = (value) =>{
        API.getReimbursementReminderAS(value).then( res =>{
            const {status,conditions} = res;
            if(status === '1'){
                this.search.form.initForm(conditions, this.search.path);
                this.search.conditions = conditions;
                this.options = res.conditions[0].items[0].options;
            }else{
                Bubble.error(res.errorInfo)
            }
        }) 
    }
    
    //获取是否报销 数据
    getReimbursementReminderAll = (value={}) =>{
        this.loading = true;
        API.getReimbursementReminderAll(value).then( res =>{
            const {status,remindList} = res;
            if(status === '1'){
                this.reimbursable = res.reimbursable;
                this.reimbursed = res.reimbursed;
                if(remindList){
                    this.remindList.setState(remindList);

                    //存储所有 数据 id
                    // for(let i=0;i<remindList.data.length;i++){
                    //     this.allAccountData.push(remindList.data.id)
                    // }
                }else{
                    this.remindList = new ListPage.Store();
                }
               
            }else{
                Bubble.error(res.errorInfo)
            }
            this.loading = false;
        })
    }

    //获取发起报销  选项
    getWorkflowList = (value={},callBack) =>{
        // this.loading = true;
        API.getWorkflowList(value).then( res =>{
            const {status,workflowSelect} = res;
            if(status === '1'){
                this.workflowSelect = workflowSelect;
            }else{
                Bubble.error(res.errorInfo)
            }

            // this.loading = false;
        })
    }

    //发起流程
    creatWorkflow = (value,callBack) =>{
        this.loading = true;
        API.creatWorkflow(value).then( res =>{
            const {status,workflowSelect} = res;
            if(status === '1'){
                callBack && callBack({
                    requestId:res.requestId
                })
            }else{
                Bubble.error(res.errorInfo)
            }

            this.loading = false;
        })
    }

    //删除报销提醒
    doDeleteRemind = (value,callBack) =>{
        this.loading = true;
        API.doDeleteReimbursementReminder(value).then( res=>{
            const {status} = res;
            if(status === '1'){
                Bubble.success(getLabel(20461, "删除成功"));
            }else{
                Bubble.error(res.errorInfo)
            }
            callBack && callBack();
        })
    }    


    //根据传出来的数据拼接 key - value
    joinObj = (data,k) =>{
        let _obj = {};
        data.map( item =>{
            _obj[item.key] = item[k]
        })
      
        return _obj
    }

    //快速搜索 name
    setNameQuery = (value='')=>{
        this.nameQuery = value;
    }

    changeRoute = (key) =>{
        this.routeData = new RouteLayout.Store({id:key ,route:this.baseRoute})
    }

    getBaseRoute = (comObj) =>{
        this.comObj = comObj;
        this.baseRoute = comObj.props.match.path;
        this.search = new SearchAdvanced.Store({ route:this.baseRoute});
    }

    @action
    setDateParams = (value={}) =>{
        this.dateParams = Object.assign(this.dateParams,value);
    }

    setSelectedIndex = (value) =>{
        this.selectedIndex = value;
    }

    setMark = (value={},initMark) =>{
        if(initMark){
            this.mark = {time:0,amount:'',costType:''};
        }else{
            this.mark = Object.assign(this.mark,{...value});
        }
        
    }


    //类型选中
    setTypeMark = (value={}) =>{
        this.typeMark = Object.assign(this.typeMark,{...value});
    }

    //重置
    resetType = () =>{
        this.typeMark = this.initType;
    }

    //更改remember中被选中的list
    setRememberChecked = (value={})=>{
        this.rememberChecked = Object.assign(this.rememberChecked,{...value});
    }

    //重置表单
    resetForm = () =>{
        this.search.form.resetFields();
    }

    //重置list数据
    resetList = ()=>{
        this.remindList = new ListPage.Store();
        this.allAccountData = [];
    }

    //存储打开流程信息
    setWfInfo = (value) =>{
        this.wfInfo = value;
    }

    @action
    changeSearchValue = (val) => {
        this.nameQuery = val;
    };

    //清除数据
    clearData = () =>{
        this.resetList();
        this.resetForm();
        this.nameQuery = '';
        this.setMark({},true);
    }

    //保存跳转到 记一笔详情页面的 参数
    setToRememberDetailPageData = (value)=>{
        this.toRememberDetailPageData = value;
    }

    
}